package pacBioSimple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class PacBioFastaMSAContainer {

	List<HashMap<Character, Integer>> msa = null;
	List<Character>maxima_val = null;
	int sequenceNumber=0;
	Random dice2Face = new Random();

	public StringBuilder getConsensusSequence()
	{
		StringBuilder sb = new StringBuilder();
		
		for(Character c : maxima_val)
		{
			if(c !='-')
			{
				sb.append(c);
			}
		}
		return sb;
	}
	
	
	public Integer getMaximumNumberConservation()
	{
		//scan through the map and get the maximum
		int conservationCounter=0;

		for(HashMap<Character, Integer> posMap : msa)
		{
			for(Map.Entry<Character, Integer> bases: posMap.entrySet())
			{
				if(bases.getValue()==sequenceNumber)
				{
					++conservationCounter;
				}
			}
		}
		return conservationCounter;
	}
	
	public Integer getMaximumNumberConservation(double degree)
	{
		//scan through the map and get the maximum
		int conservationCounter=0;

		if(degree <=0)
		{
			degree=100;
		}
		for(HashMap<Character, Integer> posMap : msa)
		{
			for(Map.Entry<Character, Integer> bases: posMap.entrySet())
			{
				int val = (int) Math.round(sequenceNumber*degree/100);
				if(bases.getValue()>=((int) Math.round(sequenceNumber*degree/100)))
				{
					++conservationCounter;
				}
			}
		}
		return conservationCounter;
	}
	
	public void readFastaFile(String fasta)
	{
		++sequenceNumber;
		
		if(msa ==null)
		{
			maxima_val= new ArrayList<Character>();
			
			msa = new ArrayList<HashMap<Character,Integer>>();
			for(int i =0; i < fasta.length();++i)
			{
				HashMap<Character, Integer> pos=new HashMap<Character, Integer>();
				
				if('A' == fasta.charAt(i) || 'a' == fasta.charAt(i) )
				{
					pos.put('A',1);
					pos.put('C',0);
					pos.put('G',0);
					pos.put('T',0);
					pos.put('-',0);
					maxima_val.add('A');
				}
				else if('C' == fasta.charAt(i)|| 'c' == fasta.charAt(i))
				{
					pos.put('A',0);
					pos.put('C',1);
					pos.put('G',0);
					pos.put('T',0);
					pos.put('-',0);
					maxima_val.add('C');
				}
				else if('G' == fasta.charAt(i)||'g' == fasta.charAt(i))
				{
					pos.put('A',0);
					pos.put('C',0);
					pos.put('G',1);
					pos.put('T',0);
					pos.put('-',0);
					maxima_val.add('G');
				}
				else if('T' == fasta.charAt(i)|| 't' == fasta.charAt(i))
				{
					pos.put('A',0);
					pos.put('C',0);
					pos.put('G',0);
					pos.put('T',1);
					pos.put('-',0);
					maxima_val.add('T');
				}
				else if('-' == fasta.charAt(i))
				{
					pos.put('A',0);
					pos.put('C',0);
					pos.put('G',0);
					pos.put('T',0);
					pos.put('-',1);
					maxima_val.add('-');
				}
				msa.add(pos);
			}
		}
		else
		{
			for(int i=0; i < fasta.length();++i)
			{
				msa.get(i).put(fasta.charAt(i),msa.get(i).get(fasta.charAt(i))+1);
				if(fasta.charAt(i)=='-' && maxima_val.get(i) != '-')
				{
					int internal_counter=i;
					while(internal_counter<fasta.length())
					{
						if(fasta.charAt(internal_counter) !='-' ||  maxima_val.get(internal_counter) == '-')
						{
							break;
						}
						++internal_counter;
					}						
					if(internal_counter>=5)
					{
						i+=internal_counter;						
					}						
					else
					{
						if(msa.get(i).get(fasta.charAt(i)) > msa.get(i).get(maxima_val.get(i)))
						{
							maxima_val.set(i,fasta.charAt(i));
						}
					}
				}
				else if(maxima_val.get(i) == '-' && fasta.charAt(i)!='-')
				{
					int internal_counter=i;
					while(internal_counter<fasta.length())
					{
						if(fasta.charAt(internal_counter) =='-' ||  maxima_val.get(internal_counter) != '-')
						{
							break;
						}
						++internal_counter;
					}						
					if(internal_counter>=5)
					{
						
						for(;i<internal_counter;++i)
						{
							maxima_val.set(i,fasta.charAt(i));
						}
					}
					else
					{
						if(msa.get(i).get(fasta.charAt(i)) >= msa.get(i).get(maxima_val.get(i)))
						{
							maxima_val.set(i,fasta.charAt(i));
						}
					}
				}
				else
				{	
					if(msa.get(i).get(fasta.charAt(i)) > msa.get(i).get(maxima_val.get(i)))
					{
						maxima_val.set(i,fasta.charAt(i));
					}
					else if(msa.get(i).get(fasta.charAt(i)) == msa.get(i).get(maxima_val.get(i)))
					{
						if(dice2Face.nextInt(100)+1>50)
						{
							maxima_val.set(i,fasta.charAt(i));
						}
					}
						
				}
			}
		}
		
	}
	
	
	
}
